package com.ggs.rg.ybjb.usermodule.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.pojomodule.pojo.TAddress;
import com.ggs.rg.ybjb.pojomodule.pojo.TUser;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import com.ggs.rg.ybjb.pojomodule.util.constants.ErrorEnum;
import com.ggs.rg.ybjb.usermodule.service.TAddressService;
import com.ggs.rg.ybjb.usermodule.service.UserService;
import com.ggs.rg.ybjb.usermodule.util.AddressUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;


@RestController
@RequestMapping("/address")
public class TAddressController {

    @Autowired
    TAddressService tAddressService;

    @Autowired
    UserService userService;

    //增加收货地址
    @PostMapping("addAddress")
    public JSONObject addAddress(@RequestBody(required = false) JSONObject address){
        if (address == null)
            return CommonUtil.errorJson(ErrorEnum.E_30002);
        CommonUtil.hasAllRequired(address,"phone,address,consigneeName,consigneePhone");
        if (StringTools.isNullOrEmpty(userService.getById(address.getString("phone"))))
            return CommonUtil.sqlErrorJson("没有查询到手机号为:" + address.getString("phone") + "的用户");
        TAddress userAddress = new TAddress(address.getString("address"),
                                            address.getString("phone"),
                                            address.getString("consigneeName"),
                                            address.getString("consigneePhone"));
        boolean result = tAddressService.save(userAddress);
        if (result)
            return CommonUtil.successJson();
        else
            return CommonUtil.errorJson(ErrorEnum.E_100);
    }

    //根据id删除收货地址
    @PostMapping("deleteAddressByAid")
    public JSONObject deleteAddressByAid(@RequestBody(required = false) JSONObject params){
        if (StringTools.isNullOrEmpty(params))
            return CommonUtil.errorJson(ErrorEnum.E_90003);
        CommonUtil.hasAllRequired(params,"aid");
        Integer aid = params.getInteger("aid");
        boolean result = tAddressService.removeById(aid);
        if (result)
            return CommonUtil.successJson();
        else
            return CommonUtil.errorJson(ErrorEnum.E_100);
    }

    //根据id更新收货地址
    @PostMapping("updateAddressByAid")
    public JSONObject updateAddressByAid(@RequestBody(required = false) JSONObject address){
        if (address == null)
            return CommonUtil.errorJson(ErrorEnum.E_30002);
        CommonUtil.hasAllRequired(address,"aid,address,consigneeName,consigneePhone");
        TAddress newAddress = new TAddress(address.getInteger("aid"),
                address.getString("address"),
                address.getString("consigneeName"),
                address.getString("consigneePhone"));
        boolean result = tAddressService.updateById(newAddress);
        if (result)
            return CommonUtil.successJson();
        else
            return CommonUtil.errorJson(ErrorEnum.E_100);
    }

    //查询用户所有收货地址
    @GetMapping("getAddress")
    public JSONObject getAddress(@RequestParam(required = false) String phone){
        if (StringTools.isNullOrEmpty(phone))
            return CommonUtil.errorJson(ErrorEnum.E_90003);
        QueryWrapper<TAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.select("aid","address","consignee_name","consignee_phone").eq("phone",phone);
        List<TAddress> addressList = tAddressService.list(queryWrapper);
        if (addressList.size() <= 0)
            return CommonUtil.sqlErrorJson("手机号为:" + phone + "的用户不存在!");
        List<JSONObject> result = AddressUtil.addressForJson(addressList);
        return CommonUtil.successJson(result);
    }
}

